package com.djl.sqtg.controller;

import com.djl.sqtg.entity.Cart;
import com.djl.sqtg.entity.CartVO;
import com.djl.sqtg.service.ICartService;
import com.djl.sqtg.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController extends BaseController{

    @Autowired
    private ICartService cartService;

    @GetMapping({"getCartByUid/{uid}"})
    public JsonResult<List<CartVO>> getCartByUid(@PathVariable("uid") Integer uid){
        List<CartVO> data=cartService.getCartByUid(uid);
        System.out.println(data);
        return new JsonResult<List<CartVO>>(OK,data);
    }

    @GetMapping({"updateCartState/{uid}/{cid}/{goodsState}"})
    public JsonResult<List<CartVO>> updateCartState(@PathVariable("uid") Integer uid,@PathVariable("cid") Integer cid,@PathVariable("goodsState") String goodsState){
        System.out.println("uid:"+uid+",cid:"+cid+",goodsState:"+goodsState);
        Integer goods_state;
        if (goodsState.equals("false")){
            System.out.println("gooodsState为false");
            goods_state=0;
        }else {
            System.out.println("gooodsState为true");
            goods_state=1;
        }
       cartService.updateCartState(cid,goods_state);
        return new JsonResult(OK);
    }

    @GetMapping("deletCartByCid/{cid}")
    public JsonResult deletCartByCid(@PathVariable("cid")  Integer cid) {
        System.out.println(cid);
        cartService.deletCartByCid(cid);
        return new JsonResult(OK);
    }

    @GetMapping("addCart/{uid}/{gid}")
    public JsonResult addCart(@PathVariable("uid") Integer uid, @PathVariable("gid")  Integer gid) {
        System.out.println("加入购物车uid:"+uid+"   ，gid:"+gid);
//        返回的是该条数据在数据表中的id号，若数据表中无此数据则会返回null
//        System.out.println("cid:"+ cartService.findCartByUidGid(uid,gid));
        Cart cart=cartService.findCartByUidGid(uid,gid);
        System.out.println(cart);
        if (cart!=null){
//            当前商品的数量加1
            cartService.updateCartNum(cart.getUid(), cart.getGid(),cart.getNum()+1);
        }else{
            cartService.addCart(uid,gid);
        }
        return new JsonResult(OK);
    }


    @GetMapping("updateCartNumBycid/{cid}/{num}")
    public JsonResult updateCartNumBycid(@PathVariable("cid") Integer cid,@PathVariable("num")  Integer num) {
        System.out.println(cid+","+num);
        cartService.updateCartNumBycid(cid,num);
        return new JsonResult(OK);
    }

}
